package com.ybkj.o2o.mc.service.impl;


import com.ybkj.o2o.mc.entity.PassengerAddress;
import com.ybkj.o2o.mc.mapper.PassengerAddressMapper;
import com.ybkj.o2o.mc.service.PassengerAddressService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by shine on 15/8/19.
 */
@Service
public class DefaultPassengerAddressService implements PassengerAddressService {

  @Autowired
  private PassengerAddressMapper passengerAddressMapper;


  @Override
  public boolean exists(String address, Integer type, Long passengerId) {

    PassengerAddress passengerAddress = passengerAddressMapper
            .selectByAddressAndTypeAndPassenger(address, type, passengerId);

    return null != passengerAddress;
  }


  @Override
  public int insert(PassengerAddress passengerAddress) {

    passengerAddress.setCreated(new Date());
    passengerAddress.setUpdated(new Date());
    passengerAddress.setVersion(1);

    return passengerAddressMapper.insert(passengerAddress);
  }

  @Override
  public int delete(Long addressId, Long passengerId) {

    return passengerAddressMapper.delete(addressId, passengerId);
  }

  @Override
  public PassengerAddress find(Long addressId, Long passengerId) {

    return passengerAddressMapper.selectByIdAndPassenger(addressId, passengerId);
  }

  @Override
  public int update(PassengerAddress passengerAddress) {

    passengerAddress.setUpdated(new Date());

    return passengerAddressMapper.updateByPrimaryKey(passengerAddress);
  }

  @Override
  public PassengerAddress findByPassengerAndType(Long passengerId, Integer type) {

    return passengerAddressMapper.findByPassengerAndType(passengerId, type);
  }


  @Override
  public List<PassengerAddress> findByPassenger(Long passengerId) {

    return passengerAddressMapper.selectByPassenger(passengerId);
  }

  @Override
  public List<PassengerAddress> findByPassenger(Long passengerId, int offset, int rows) {

    return passengerAddressMapper.selectByPassengerForPage(passengerId, offset, rows);
  }

  @Override
  public long countByPassenger(Long passengerId) {

    return passengerAddressMapper.countByPassenger(passengerId);
  }

  @Override
  public int deleteByPassenger(Long passengerId) {

    return passengerAddressMapper.deleteByPassengerId(passengerId);

  }

  @Override
  public int deleteByPassengers(List<Long> passengers) {

    return passengerAddressMapper.deleteByPassengers(passengers);
  }
}
